//
//  FileSizeFormatter.m
//  RFA Manager
//
//  Created by Torsten Kammer on 03.03.08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "FileSizeFormatter.h"


@implementation FileSizeFormatter

+ (NSString *)stringForNumber:(id)obj;
{	
	NSUInteger bytes = [obj integerValue];
	
	// If it's less than a KB, give byte value
	if (bytes < 1024) return [NSString stringWithFormat:NSLocalizedString(@"%u Bytes", @"size in bytes"), bytes];
	// Kilobytes are given straight
	else if (bytes < 1024*1024)
	{
		NSUInteger kilobytes = (NSUInteger) ceil((double) bytes / 1024.0f);
		return [NSString stringWithFormat:NSLocalizedString(@"%u KB", @"size in kilobytes (kibibytes to be precise)"), kilobytes];
	}
	// Megabytes have a comma
	else if (bytes < 1024*1024*1024)
	{
		double megabytes = (double) bytes / (1024.0f * 1024.0f);
		return [NSString stringWithFormat:NSLocalizedString(@"%.1f MB", @"size in megabytes (mebibytes to be precise)"), megabytes];
	}
	// Gigabyte have two decimals. There are no larger numbers in an RFA file possible.
	else
	{
		double gigabytes = (double) bytes / (1024.0f * 1024.0f * 1024.0f);
		return [NSString stringWithFormat:NSLocalizedString(@"%.2f GB", @"size in gigabytes (gibibytes to be precise)"), gigabytes];
	}
}

- (NSAttributedString *)attributedStringForObjectValue:(id)obj withDefaultAttributes:(NSDictionary *)attrs;
{
	return [[NSAttributedString alloc] initWithString:[self stringForObjectValue:obj] attributes:attrs];
}

- (NSString *)stringForObjectValue:(id)obj
{
	return [[self class] stringForNumber:obj];
}

@end
